package com.example.demo.plugin;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.codegen.mybatis3.ListUtilities;
import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;
import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.AbstractXmlElementGenerator;

public class ConditionDataColumnElementGenerate extends AbstractXmlElementGenerator {
    public ConditionDataColumnElementGenerate() {
        super();
    }

    @Override
    public void addElements(XmlElement parentElement) {
        XmlElement answer = new XmlElement("sql"); //$NON-NLS-1$

        answer.addAttribute(new Attribute("id", //$NON-NLS-1$
                "QUERY_WHERE_CLAUSE_CONDITION"));

        context.getCommentGenerator().addComment(answer);

        XmlElement whereElement = new XmlElement("where");
        answer.addElement(whereElement);
        StringBuilder sb = new StringBuilder();
        IntrospectedColumn deleteColumn = introspectedTable.getColumn("IS_DEL");
        if (deleteColumn != null){
            sb.append(" IS_DEL = 0");
        }else {
            sb.append(" 1=1");
        }
        whereElement.addElement(new TextElement(sb.toString()));

        for (IntrospectedColumn introspectedColumn : ListUtilities.removeGeneratedAlwaysColumns(introspectedTable
                .getNonBLOBColumns())) {

            sb.setLength(0);
            sb.append(introspectedColumn.getJavaProperty());
            sb.append(" != null"); //$NON-NLS-1$
            if (introspectedColumn.getJdbcTypeName() == "VARCHAR") {
                sb.append(" and ");
                sb.append(introspectedColumn.getJavaProperty());
                sb.append(" != '' "); //$NON-NLS-1$
            }
            XmlElement isNotNullElement = new XmlElement("if"); //$NON-NLS-1$
            isNotNullElement.addAttribute(new Attribute("test", sb.toString())); //$NON-NLS-1$
            whereElement.addElement(isNotNullElement);

            sb.setLength(0);
//            sb.append("<![CDATA[ AND `");
            sb.append("<![CDATA[ AND ");
            sb.append(MyBatis3FormattingUtilities
                    .getEscapedColumnName(introspectedColumn));
            if (introspectedColumn.getJdbcTypeName() == "VARCHAR"){
//                sb.append("` LIKE ");
                sb.append(" LIKE ");
                sb.append("concat('%',");
                sb.append(MyBatis3FormattingUtilities
                        .getParameterClause(introspectedColumn));
                sb.append(",'%')");
            }else {
//                sb.append("` = "); //$NON-NLS-1$
                sb.append(" = "); //$NON-NLS-1$
                sb.append(MyBatis3FormattingUtilities
                        .getParameterClause(introspectedColumn));
            }

            sb.append(" ]]>");

            isNotNullElement.addElement(new TextElement(sb.toString()));
        }


        if (context.getPlugins().sqlMapBaseColumnListElementGenerated(
                answer, introspectedTable)) {
            parentElement.addElement(answer);
        }
    }


}
